<?php
require_once('vendor/autoload.php');
$document = SetaPDF_Core_Document::loadByFilename('12.pdf');
$isEncrypted = $document->hasSecHandler();
if ($isEncrypted) {
    // get the security handler
    $secHandler = $document->getSecHandler();

    // authenticate with a password without knowing if it is the owner or user password:
    if ($secHandler->auth('a secret password')) {
        echo 'authenticated as ' . $secHandler->getAuthMode();
    } else {
        echo 'authentication failed - neither user nor owner password did match.';
    }

    // authenticate with the user password:
    if ($secHandler->authByUserPassword('a secret password')) {
        echo 'authenticated as user';
    } else {
        echo 'authentication failed with the user password.';
    }

    // authenticate with the owner password:
    if ($secHandler->authByOwnerPassword('a secret password')) {
        echo 'authenticated as owner';
    } else {
        echo 'authentication failed with the owner password.';
    }
}
if ($secHandler->getAuthMode() === SetaPDF_Core_SecHandler::OWNER) {
    $document->setSecHandler(null);

    $writer = new SetaPDF_Core_Writer_File('not-encrypted.pdf');
    $document->setWriter($writer);
    $document->save()->finish();
}